package com.liugh.asyn;

/**
 * 异步job执行结果
 */
public class AsyncJobResult<T> {

    /**
     * 无效的异步任务执行结果
     */
    public static final AsyncJobResult INVALID_RESULT =
            new AsyncJobResult<>(null, new AsyncJobException("无效的异步执行结果！"));



    /**
     * 结果数据，执行成功后获取
     */
    private T resultData;

    /**
     * 执行失败异常
     */
    private AsyncJobException resultException;

    public AsyncJobResult() {
    }

    public AsyncJobResult(T resultData) {
        this.resultData = resultData;
    }

    public AsyncJobResult(T resultData, AsyncJobException resultException) {
        this.resultData = resultData;
        this.resultException = resultException;
    }

    /**
     * 是否成功执行
     * @return
     */
    public boolean isExecuteSuccess() {
        return null == resultException;
    }

    /**
     * 获取执行结果
     * @return
     * @throws AsyncJobException 如果是失败的结果，直接抛出异常
     */
    public T getResult() throws AsyncJobException {
        if (isExecuteSuccess()) {
            return resultData;
        } else {
            throw resultException;
        }
    }



    public T getResultData() {
        return resultData;
    }

    public void setResultData(T resultData) {
        this.resultData = resultData;
    }

    public Exception getResultException() {
        return resultException;
    }

    public void setResultException(AsyncJobException resultException) {
        this.resultException = resultException;
    }



}
